import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
// import './assets/css/global.scss'

console.log('main.js loaded')

// 全局错误处理器，过滤ResizeObserver错误
const originalError = console.error
console.error = function(...args) {
  const message = args[0]
  if (typeof message === 'string' && (
    message.includes('ResizeObserver loop limit exceeded') ||
    message.includes('ResizeObserver loop completed with undelivered notifications')
  )) {
    return // 忽略ResizeObserver错误
  }
  originalError.apply(console, args)
}

// 全局未捕获错误处理
window.addEventListener('error', (event) => {
  if (event.message && (
    event.message.includes('ResizeObserver loop limit exceeded') ||
    event.message.includes('ResizeObserver loop completed with undelivered notifications')
  )) {
    event.preventDefault()
    return false
  }
})

// 临时调试功能：清除用户数据并重新获取
window.debugClearUserData = () => {
  localStorage.clear()
  location.reload()
}

// 临时调试功能：强制刷新用户信息
window.debugRefreshUserInfo = async () => {
  const store = app.config.globalProperties.$store
  try {
    await store.dispatch('user/getUserInfo')
    console.log('用户信息刷新成功:', store.getters.userInfo)
    console.log('用户角色:', store.getters.roles)
  } catch (error) {
    console.error('刷新用户信息失败:', error)
  }
}

const app = createApp(App)

// 注册所有图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(store)
app.use(router)
app.use(ElementPlus)

console.log('About to mount app')
app.mount('#app')
console.log('App mounted')
